//
//  NSString+Extension.m
//  DSJY
//
//  Created by 冯华强 on 16/10/21.
//  Copyright © 2016年 dsjy. All rights reserved.
//

#import "NSString+Extension.h"
#import "RegexKitLite.h"

@implementation NSString (Extension)

- (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)sizeWithFont:(UIFont *)font
{
    return [self sizeWithFont:font maxW:MAXFLOAT];
}

- (NSString *)specialFormat
{
//    NSString *str = [self stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

+ (NSString*)timeformatFromSeconds:(NSInteger)seconds
{
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld", (seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld", seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@", str_minute, str_second];
    return format_time;
}

/**
 *  手机号码的有效性判断
 *  检测是否是手机号码
 */
- (BOOL)isMobileNumber
{
    /**
     * 手机号码
     * 移动：134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通：130,131,132,145,152,155,156,1709,171,176,185,186
     * 电信：133,134,153,1700,177,180,181,189
     */
    NSString * MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     */
    NSString * CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[0]|7[8]|8[2-478])\\d{8}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,145,152,155,156,1709,171,176,185,186
     */
    NSString * CU = @"^1(3[0-2]|4[5]|5[56]|709|7[1]|7[6]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,134,153,1700,177,180,181,189
     */
    NSString * CT = @"^1(3[34]|53|77|700|8[019])\\d{8}$";
    /**
     * 大陆地区固话及小灵通
     * 区号：010,020,021,022,023,024,025,027,028,029
     * 号码：七位或八位
     */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:self] == YES)
        || ([regextestcm evaluateWithObject:self] == YES)
        || ([regextestct evaluateWithObject:self] == YES)
        || ([regextestcu evaluateWithObject:self] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

//去除首尾空格
- (NSString *)trim
{
    return [self stringByTrimmingCharactersInSet:
            [NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

//验证正则
- (BOOL)regexPattern:(NSString *)pattern
{
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSArray *results = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    return results.count > 0;
}

/**
 *  传入正则表达式，一般用于限制特殊字符
 */
- (NSString *)doNotWantString:(NSString *)string block:(void (^)())block
{
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:string];
    NSArray *array = [self componentsSeparatedByCharactersInSet: doNotWant];
    if (array.count > 1) {
        if (block) {
            block();
        }
    }
    return [array componentsJoinedByString:@""];
}

/**
 *  正则表达式，控制中文、字母、数字等
 */
- (NSString *)regexDoNotWantPattern:(NSString *)pattern block:(void (^)())block
{
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSArray *results = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    NSMutableString *stringM = [NSMutableString string];
    for (NSTextCheckingResult *result in results) {
        [stringM appendString:[self substringWithRange:result.range]];
    }
    if (self.length != stringM.length) {
        if (block) {
            block();
        }
    }
    return stringM;
}

//是否不为空
+ (BOOL)isNotEmptyWithStr:(NSString *)str
{
    return str && [[str trim] length] != 0;
}

//是否为空
+ (BOOL)isEmptyWithStr:(NSString *)str
{
    return !str || [[str trim] length] == 0;
}

@end
